using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Info.Basic;
using EntLibEC.Biz;
using EntLibEC.Biz.Basic;

namespace EntLibEC.View.Basic
{
	/// <summary>
	/// Summary description for ShipTypeAreaPriceSearch.
	/// </summary>
	public partial class ShipTypeAreaPriceSearch : PageBase
	{

	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			DataGrid1.PageSize = AppConst.PageSize;
			DataGrid1.PagerStyle.PageButtonCount = AppConst.PageButtonCount;

			btnAdd.Attributes["onclick"]= "javascript:openWindowS3('ShipTypeAreaPriceOpt.aspx?opt=add'); return false;";

			if ( !Page.IsPostBack )
			{
				scArea.BindArea();
				scShipType.BindShipType(true, chkShipTypeOnlineShow.Checked);
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
			this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);

		}
		#endregion

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			BindGrid();
		}

		private void BindGrid()
		{
			DataTable dt = BuildTable();
			base.SetDataGridPage(dt, DataGrid1);

			DataGrid1.DataSource = dt;
			DataGrid1.DataBind();

			int rows = 0;
			if ( Util.HasMoreRow(dt) )
				rows = dt.Rows.Count;
			Assert(lblMessage, rows.ToString() + " record(s)", 1);
		}
		private DataTable BuildTable()
		{
			DataTable table = new DataTable("ShipAreaPrice");
			table.Columns.Add(new DataColumn("SysNo",typeof(int)));
			table.Columns.Add(new DataColumn("ShipTypeName",typeof(string)));
			table.Columns.Add(new DataColumn("AreaFullName",typeof(string)));
			table.Columns.Add(new DataColumn("BaseWeight",typeof(string)));
			table.Columns.Add(new DataColumn("TopWeight",typeof(string)));
			table.Columns.Add(new DataColumn("UnitWeight",typeof(string)));
			table.Columns.Add(new DataColumn("UnitPrice",typeof(string)));
			table.Columns.Add(new DataColumn("MaxPrice",typeof(string)));
			
			Hashtable priceHt = ASPManager.GetInstance().GetShipAreaPriceHash();

			if ( priceHt == null || priceHt.Count == 0 )
				return table;

			Hashtable areaHt = ASPManager.GetInstance().GetAreaHash();
			Hashtable shipTypeHt = ASPManager.GetInstance().GetShipTypeHash();

			string scAreaName=AppConst.StringNull;
			if ( scArea.AreaSysNo != AppConst.IntNull)
			{
				scAreaName = ((AreaInfo)areaHt[scArea.AreaSysNo]).GetWholeName();
			}


			foreach(ShipAreaPriceInfo item in priceHt.Values)
			{

				AreaInfo itemArea = ((AreaInfo)areaHt[item.AreaSysNo]);

				if ( (  scAreaName == AppConst.StringNull || itemArea.GetWholeName().IndexOf(scAreaName)!=-1)
					&& ( scShipType.ShipTypeSysNo == AppConst.IntNull || scShipType.ShipTypeSysNo == item.ShipTypeSysNo)
                    && shipTypeHt.ContainsKey(item.ShipTypeSysNo)
					)
				{
				
					DataRow row = table.NewRow();
					row["SysNo"] = item.SysNo;
					row["ShipTypeName"] = ((ShipTypeInfo)shipTypeHt[item.ShipTypeSysNo]).ShipTypeName;
					row["AreaFullName"] = ((AreaInfo)areaHt[item.AreaSysNo]).GetWholeName();
					row["BaseWeight"] = item.BaseWeight.ToString();
					row["TopWeight"] = item.TopWeight.ToString();
					row["UnitWeight"] = item.UnitWeight.ToString();
					row["UnitPrice"] = item.UnitPrice.ToString(AppConst.DecimalFormat);
					row["MaxPrice"] = item.MaxPrice.ToString(AppConst.DecimalFormat);

					table.Rows.Add(row);
				}
			}

			return table;
		}

		private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.AreaShipPay, false, lblMessage) )
				return;

			if ( e.CommandName == "delete" )
			{
				int priceSysNo = (int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
				try
				{
					ASPManager.GetInstance().DeleteShipAreaPrice(priceSysNo);
					LogManager.GetInstance().Write(new LogInfo(priceSysNo, (int)AppEnum.LogType.Basic_ShipAreaPrice_Delete, base.sInfo));
					Assert(lblMessage, "Remove the Ship Area Price record ok", 1);
				}
				catch(BizException exp)
				{
					Assert(lblMessage, exp.Message, -1);
				}
				catch(Exception exp)
				{
					Assert(lblMessage, "Remove the Ship Area Price record failed, see log", -1);
					ErrorLog.GetInstance().Write(exp.ToString());
				}
				BindGrid();
			}
		}

		private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			DataGrid1.CurrentPageIndex = e.NewPageIndex;
			BindGrid();
		}

        protected void chkShipTypeOnlineShow_CheckedChanged(object sender, EventArgs e)
        {
            scShipType.BindShipType(true, chkShipTypeOnlineShow.Checked);
        }
	}
}
